package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //跳跃游戏
    public boolean canJump(int[] nums) {
        int n = nums.length;
        int left = 0;
        int right = 0;
        int max = Integer.MIN_VALUE;
        while (left <= right ) {
            if(max>=n-1) return true;
            for (int i = left; i <= right; i++) {
                max = Math.max(max,i+nums[i]);
            }
            left = right+1;
            right = max;
        }
        return false;
    }

    //可以被一步捕获的棋子数
    public int numRookCaptures(char[][] board) {
        int n = board.length, res = 0, x = 0, y = 0;
        for (int i = 0; i < n; i++) {
            boolean q = false;
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'R') {
                    x = i;
                    y = j;
                }
            }
        }
        int x1 = x,x2 = x,y1 = y,y2 = y;
        while (x1 >= 0 || x2 < 8 || y1 >= 0 || y2 < 8) {
            if (x1 >= 0) {
                if (board[x1][y] == 'B') {
                    x1 = -1;
                }else if (board[x1][y] == 'p') {
                    res++;
                    x1 = -1;
                }
                x1--;
            }
            if (x2 < 8) {
                if (board[x2][y] == 'B') {
                    x2 = 8;
                }else if (board[x2][y] == 'p') {
                    res++;
                    x2 = 8;
                }
                x2++;
            }
            if (y1 >= 0) {
                if (board[x][y1] == 'B') {
                    y1 = -1;
                }else if (board[x][y1] == 'p') {
                    res++;
                    y1 = -1;
                }
                y1--;
            }
            if (y2 < 8) {
                if (board[x][y2] == 'B') {
                    y2 = 8;
                }else if (board[x][y2] == 'p') {
                    res++;
                    y2 = 8;
                }
                y2++;
            }
        }
        return res;
    }
}
